package com.fbs.wowo.finance.mapper;

import com.fbs.wowo.finance.entity.Payment;
import com.fbs.wowo.finance.vo.Buy;
import com.fbs.wowo.finance.vo.BuyEssay;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;

public interface PaymentMapper {
    @Delete({
        "delete from payment",
        "where id = #{id,jdbcType=VARCHAR}"
    })
    int deleteByPrimaryKey(String id);

    @InsertProvider(type=PaymentSqlProvider.class, method="insertSelective")
    int insertSelective(Payment record);

    @Select({
        "select",
        "id, body, trade_no, transaction_id, buyer_id, total_fee, time_start, trade_type, ",
        "product_id, product_category, product_type, status",
        "from payment",
        "where id = #{id,jdbcType=VARCHAR}"
    })
    @Results({
        @Result(column="id", property="id", jdbcType=JdbcType.VARCHAR, id=true),
        @Result(column="body", property="body", jdbcType=JdbcType.VARCHAR),
        @Result(column="trade_no", property="tradeNo", jdbcType=JdbcType.VARCHAR),
        @Result(column="transaction_id", property="transactionId", jdbcType=JdbcType.VARCHAR),
        @Result(column="buyer_id", property="buyerId", jdbcType=JdbcType.VARCHAR),
        @Result(column="total_fee", property="totalFee", jdbcType=JdbcType.DECIMAL),
        @Result(column="time_start", property="timeStart", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="trade_type", property="tradeType", jdbcType=JdbcType.TINYINT),
        @Result(column="product_id", property="productId", jdbcType=JdbcType.VARCHAR),
        @Result(column="product_category", property="productCategory", jdbcType=JdbcType.TINYINT),
        @Result(column="product_type", property="productType", jdbcType=JdbcType.TINYINT),
        @Result(column="status", property="status", jdbcType=JdbcType.TINYINT)
    })
    Payment selectByPrimaryKey(String id);

    @UpdateProvider(type=PaymentSqlProvider.class, method="updateByPrimaryKeySelective")
    int updateByPrimaryKeySelective(Payment record);

    /**
     * 按订单id查询
     */
    @Select({
            "select",
            "id, body, trade_no, transaction_id, buyer_id, total_fee, time_start, trade_type, ",
            "product_id, product_category, product_type, status",
            "from payment",
            "where trade_no = #{tradeNo,jdbcType=VARCHAR}"
    })
    @Results({
            @Result(column="id", property="id", jdbcType=JdbcType.VARCHAR, id=true),
            @Result(column="body", property="body", jdbcType=JdbcType.VARCHAR),
            @Result(column="trade_no", property="tradeNo", jdbcType=JdbcType.VARCHAR),
            @Result(column="transaction_id", property="transactionId", jdbcType=JdbcType.VARCHAR),
            @Result(column="buyer_id", property="buyerId", jdbcType=JdbcType.VARCHAR),
            @Result(column="total_fee", property="totalFee", jdbcType=JdbcType.DECIMAL),
            @Result(column="time_start", property="timeStart", jdbcType=JdbcType.TIMESTAMP),
            @Result(column="trade_type", property="tradeType", jdbcType=JdbcType.TINYINT),
            @Result(column="product_id", property="productId", jdbcType=JdbcType.VARCHAR),
            @Result(column="product_category", property="productCategory", jdbcType=JdbcType.TINYINT),
            @Result(column="product_type", property="productType", jdbcType=JdbcType.TINYINT),
            @Result(column="status", property="status", jdbcType=JdbcType.TINYINT)
    })
    Payment findByTradeNo(String tradeNo);

    /**
     * 按 buyerId, productId, productCategory, productType唯一确定订单
     */
    @Select({
            "select",
            "id, body, trade_no, transaction_id, buyer_id, total_fee, time_start, trade_type, ",
            "product_id, product_category, product_type, status",
            "from payment",
            "where buyer_id = #{buyerId,jdbcType=VARCHAR} " +
                    "and product_id = #{productId,jdbcType=VARCHAR} " +
                    "and product_category = #{productCategory,jdbcType=TINYINT} " +
                    "and product_type = #{productType,jdbcType=TINYINT}"
    })
    @Results({
            @Result(column="id", property="id", jdbcType=JdbcType.VARCHAR, id=true),
            @Result(column="body", property="body", jdbcType=JdbcType.VARCHAR),
            @Result(column="trade_no", property="tradeNo", jdbcType=JdbcType.VARCHAR),
            @Result(column="transaction_id", property="transactionId", jdbcType=JdbcType.VARCHAR),
            @Result(column="buyer_id", property="buyerId", jdbcType=JdbcType.VARCHAR),
            @Result(column="total_fee", property="totalFee", jdbcType=JdbcType.DECIMAL),
            @Result(column="time_start", property="timeStart", jdbcType=JdbcType.TIMESTAMP),
            @Result(column="trade_type", property="tradeType", jdbcType=JdbcType.TINYINT),
            @Result(column="product_id", property="productId", jdbcType=JdbcType.VARCHAR),
            @Result(column="product_category", property="productCategory", jdbcType=JdbcType.TINYINT),
            @Result(column="product_type", property="productType", jdbcType=JdbcType.TINYINT),
            @Result(column="status", property="status", jdbcType=JdbcType.TINYINT)
    })
    Payment findByBuyerProductIdTypeCT(@Param("buyerId") String buyerId,
                                       @Param("productId") String productId,
                                       @Param("productType") Byte productType,
                                       @Param("productCategory") Byte productCategory);

    // 按用户id查询订单
    @Select({
            "select",
            "id, body, trade_no, transaction_id, buyer_id, total_fee, time_start, trade_type, ",
            "product_id, product_category, product_type, status",
            "from payment",
            "where buyer_id = #{buyerId,jdbcType=VARCHAR} and status = #{status,jdbcType=TINYINT}"
    })
    @Results({
            @Result(column="id", property="id", jdbcType=JdbcType.VARCHAR, id=true),
            @Result(column="body", property="body", jdbcType=JdbcType.VARCHAR),
            @Result(column="trade_no", property="tradeNo", jdbcType=JdbcType.VARCHAR),
            @Result(column="transaction_id", property="transactionId", jdbcType=JdbcType.VARCHAR),
            @Result(column="buyer_id", property="buyerId", jdbcType=JdbcType.VARCHAR),
            @Result(column="total_fee", property="totalFee", jdbcType=JdbcType.DECIMAL),
            @Result(column="time_start", property="timeStart", jdbcType=JdbcType.TIMESTAMP),
            @Result(column="trade_type", property="tradeType", jdbcType=JdbcType.TINYINT),
            @Result(column="product_id", property="productId", jdbcType=JdbcType.VARCHAR),
            @Result(column="product_category", property="productCategory", jdbcType=JdbcType.TINYINT),
            @Result(column="product_type", property="productType", jdbcType=JdbcType.TINYINT),
            @Result(column="status", property="status", jdbcType=JdbcType.TINYINT)
    })
    List<Payment> findByBuyerId(@Param("buyerId") String buyerId, @Param("status") Byte status);

    /**
     * 我的购买 图文
     * @param userId
     * @param productType
     * @return
     */
    @Select({
            "select",
            "a.title, a.img, a.page_view, a.intro, a.id as product_id",
            "from essay a ",
            "inner join payment p",
            "on p.product_id=a.id",
            "where p.buyer_id = #{userId,jdbcType=VARCHAR} and p.product_type = #{productType,jdbcType=TINYINT}"
    })
    @Results({
            @Result(column="product_id", property="productId", jdbcType=JdbcType.VARCHAR),
            @Result(column="title", property="title", jdbcType=JdbcType.VARCHAR),
            @Result(column="img", property="img", jdbcType=JdbcType.VARCHAR),
            @Result(column="page_view", property="pageView", jdbcType=JdbcType.INTEGER),
            @Result(column="intro", property="intro", jdbcType=JdbcType.VARCHAR)
    })
    List<Buy> findByBuyerIdPTArt(@Param("userId") String userId, @Param("productType") byte productType);

    /**
     * 我的购买 音频
     * @param userId
     * @param productType
     * @return
     */
    @Select({
            "select",
            "a.title, a.img, a.page_view, a.audio_length, a.id as product_id",
            "from essay a ",
            "inner join payment p",
            "on p.product_id=a.id",
            "where p.buyer_id = #{userId,jdbcType=VARCHAR} and p.product_type = #{productType,jdbcType=TINYINT}"
    })
    @Results({
            @Result(column="product_id", property="productId", jdbcType=JdbcType.VARCHAR),
            @Result(column="title", property="title", jdbcType=JdbcType.VARCHAR),
            @Result(column="img", property="img", jdbcType=JdbcType.VARCHAR),
            @Result(column="page_view", property="pageView", jdbcType=JdbcType.INTEGER),
            @Result(column="audio_length", property="audioLength", jdbcType=JdbcType.VARCHAR)
    })
    List<Buy> findByBuyerIdPTAud(@Param("userId") String userId, @Param("productType") byte productType);

    /**
     * 我的购买 专辑
     * @param userId
     * @param productType
     * @return
     */
    @Select({
            "select",
            "a.title, a.img, a.page_view, num.ct count, a.id as product_id",
            "from album a",
            "inner join (select count(*) ct, e.album_id album_id from essay e inner join album a on e.album_id=a.id) num",
            "on num.album_id=a.id",
            "inner join payment p",
            "on p.product_id=a.id",
            "where p.buyer_id = #{userId,jdbcType=VARCHAR} and p.product_type = #{productType,jdbcType=TINYINT}"
    })
    @Results({
            @Result(column="product_id", property="productId", jdbcType=JdbcType.VARCHAR),
            @Result(column="title", property="title", jdbcType=JdbcType.VARCHAR),
            @Result(column="img", property="img", jdbcType=JdbcType.VARCHAR),
            @Result(column="page_view", property="pageView", jdbcType=JdbcType.INTEGER),
            @Result(column="count", property="count", jdbcType=JdbcType.VARCHAR)
    })
    List<Buy> findByBuyerIdPTAlb(@Param("userId") String userId, @Param("productType") byte productType);

    /**
     * 我的购买 图文音频
     * @param userId
     * @param productType
     * @return
     */
    @Select({
            "select",
            "a.id as essay_id, a.title, a.img, a.page_view, a.intro, a.type, a.audio_length",
            "from essay a",
            "inner join payment p",
            "on p.product_id=a.id",
            "where p.buyer_id = #{userId,jdbcType=VARCHAR} and p.product_type = #{productType,jdbcType=TINYINT}"
    })
    @Results({
            @Result(column="essay_id", property="essayId", jdbcType=JdbcType.VARCHAR),
            @Result(column="title", property="title", jdbcType=JdbcType.VARCHAR),
            @Result(column="img", property="img", jdbcType=JdbcType.VARCHAR),
            @Result(column="page_view", property="pageView", jdbcType=JdbcType.INTEGER),
            @Result(column="intro", property="intro", jdbcType=JdbcType.VARCHAR),
            @Result(column="audio_length", property="audioLength", jdbcType=JdbcType.VARCHAR),
            @Result(column="type", property="type", jdbcType=JdbcType.TINYINT)
    })
    List<BuyEssay> findByBuyerIdPT(@Param("userId") String userId, @Param("productType") byte productType);

}